﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Common;
using System.Threading;

namespace BLL
{
    public class MovingPlatform
    {
        private int Pillar;
        private int Cell;
        WFNetLib.WaitingProc wp;
        Boolean bReset=false;
        Boolean bForwardMoving;
        byte MovingCells;
        public bool Moving(int Pillar,int Cell,Boolean bReset)
        {
            this.Pillar = Pillar;
            this.Cell = Cell;
            this.bReset = bReset;
            if (Cell > 100)
            {
                bForwardMoving = false;
                MovingCells = (byte)(200 - Cell + 1);
            }
            else
            {
                bForwardMoving = true;
                MovingCells = (byte)(Cell - 1);
            }
            if (MovingCells < 5)
                return true;
            WFNetLib.WaitingProcFunc wpf = new WFNetLib.WaitingProcFunc(WaitingMovingPlatform);
            wp = new WFNetLib.WaitingProc();
            bool rrr;
            if(bReset)
                rrr= wp.Execute(wpf, "通知转台回转", WFNetLib.WaitingType.None, "确定要取消么？");
            else
                rrr= wp.Execute(wpf, "通知转台旋转", WFNetLib.WaitingType.With_ConfirmCancel, "确定要取消么？");
            Thread.Sleep(4000);
            return rrr;
            
        }
        private void WaitingMovingPlatform(object LockWatingThread)
        {
            int percent = 0;
            RS485Result ret;
            while (true)
            {
                Thread.Sleep(1000);
                percent++;
                wp.SetProcessBar((percent * 10)%100);
                lock (LockWatingThread)
                {
                    PassportSerialPort PSP;
                    PSP = new PassportSerialPort((byte)(Pillar));
                    if (bReset)
                        ret = PSP.Moving(!bForwardMoving, MovingCells);
                    else
                        ret = PSP.Moving(bForwardMoving, MovingCells); 
                }
                if (ret ==RS485Result.OK)
                {
                    break;
                }
                if (wp.HasBeenCancelled())
                    break;
                Thread.Sleep(1000);
            }
        }
    }
}
